<?php

namespace App\Http\Controllers;

use App\Models\Media;
use App\Models\Member;
use App\Models\MemberCoupon;
use App\Util\Qrcode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MemberCouponController extends Controller
{
    // 用户卡券列表
    public function index(Request $request)
    {
        $_prefix = config('database.connections.mysql.prefix');
        $_type = $request->input('type', 'all');
        $_limit = $request->input('limit', [0, 10]);
        $MemberCoupon = MemberCoupon::leftJoin('coupons', 'member_coupons.coupon_id', '=', 'coupons.id')
            ->select(
                'member_coupons.id',
                'member_coupons.coupon_id',
                'member_coupons.status',
                'member_coupons.indate',
                'coupons.cover',
                'coupons.type',
                'coupons.name',
                'coupons.type_value'
            )
            ->where('member_coupons.member_id', auth('member')->id())
            ->offset($_limit[0])
            ->limit($_limit[1])
            ->orderByRaw($_prefix . "member_coupons.status ASC," . $_prefix . "member_coupons.indate = 1 OR " . $_prefix . "member_coupons.indate >= " . time() . " DESC, " . $_prefix . "member_coupons.created_at DESC");
        if ($_type != 'all') {
            $MemberCoupon = $MemberCoupon->where('coupons.type', $_type);
        }
        $MemberCoupon = $MemberCoupon->get();
        foreach ($MemberCoupon as $item) {
            $item->cover = $item->cover ? cool_media(Media::where("id", $item->cover)->value('thumb')) : '';
            $item->value = $this->getValue($item);
            $item->type_str = $this->getTypeStr($item->type);
            $_coupon_time = $this->getTimeStr($item);
            $item->time_str = $_coupon_time["time_str"];
            $item->past = $_coupon_time["past"];
        }
        return response(['data' => $MemberCoupon]);
    }

    /**
     * 卡券面值
     * @return string
     */
    private function getValue($coupon)
    {
        $_value = "";
        $_type_value = json_decode($coupon->type_value, true);
        switch ($coupon->type) {
            case 'dj':
                $_value = $_type_value["value"] . "元";
                break;
            case 'mj':
                $_value = "满" . $_type_value["face"] . "元，减" . $_type_value["value"] . "元";
                break;
            case 'zk':
                $_value = $_type_value["value"] . "折";
                break;
            case 'dh':
            case 'rc':
                $_value = $_type_value["value"];
                break;
        }
        unset($_type_value);
        return $_value;
    }

    /**
     * 卡券文字
     * @param $_type
     * @return string
     */
    private function getTypeStr($_type)
    {
        $_str_type = "";
        switch ($_type) {
            case 'dj':
                $_str_type = '代金券';
                break;
            case 'mj':
                $_str_type = '满减券';
                break;
            case 'dh':
                $_str_type = '兑换券';
                break;
            case 'zk':
                $_str_type = '折扣券';
                break;
            case 'rc':
                $_str_type = '入场券';
                break;
        }
        return $_str_type;
    }

    /**
     * 返回卡券日期相关字段
     * @param $coupon
     * @return array
     */
    private function getTimeStr($coupon)
    {
        // 卡券是否过期
        $_data = array(
            "time_str" => "",
            "past" => false
        );
        if ($coupon->indate == 1) {
            $_data["time_str"] = "永久有效";
        } else {
            $_data["time_str"] = date("Y-m-d", $coupon->indate);
            if ($coupon->indate + 86400 - 1 < time()) {
                $_data["past"] = true;
            }
        }
        return $_data;
    }

    /**
     * 获取一个卡券详情
     * @param $_id
     * @return mixed
     */
    public function show($_id)
    {
        $MemberCoupon = MemberCoupon::where("id", $_id)
            ->select("id", "status", "coupon_id", "indate")
            ->first();
        if ($MemberCoupon) {
            $MemberCoupon = $MemberCoupon->setAppends(['coupon']);
            $_response = response(["data" => $MemberCoupon]);
        } else {
            $_response = response(["message" => "卡券不存在"], 402);
        }
        return $_response;
    }

    /**
     *删除我的卡券
     */
    public function destroy($_id)
    {
        if (MemberCoupon::destroy($_id)) {
            $_response = response(['data' => 'success']);
        } else {
            $_response = response(['message' => '删除失败'], 402);
        }
        return $_response;
    }

    /**
     * 获取核销卡券url
     */
    public function checkUrl(Request $request)
    {
        $Qrcode = new Qrcode();
        $_params = $Qrcode->createParams($request->data);
        return response(['data'=>route('scan',$_params)]);
    }

    /**
     * 测试申请员工二维码链接是否过期
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function testCheckUrl(Request $request)
    {
        $Qrcode = new Qrcode();
        $_result = array("data"=>1);
        if(!$Qrcode->testQrcode($request->data)) {
            $_result = array("data"=>0);
        }
        return response($_result);
    }
}
